<template>
  <div class="admin-cart">
    <header class="header">
      <div class="container">
        <div class="logo">
          <h1>拼多多 - 购物车管理</h1>
        </div>
        <nav class="nav">
          <router-link to="/">返回首页</router-link>
          <router-link to="/admin/products">商品管理</router-link>
          <router-link to="/admin/users">用户管理</router-link>
          <router-link to="/admin/cart">购物车管理</router-link>
          <router-link to="/admin/categories">分类管理</router-link>
        </nav>
      </div>
    </header>

    <div class="container">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>购物车管理</span>
          </div>
        </template>

        <el-table :data="cartItems" style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column label="商品图片" width="100">
            <template #default="scope">
              <img :src="scope.row.image" class="product-thumb" />
            </template>
          </el-table-column>
          <el-table-column prop="username" label="用户" width="120" />
          <el-table-column prop="product_name" label="商品" />
          <el-table-column prop="price" label="单价" width="100">
            <template #default="scope">
              ¥{{ scope.row.price }}
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="数量" width="100" />
          <el-table-column prop="total_price" label="小计" width="120">
            <template #default="scope">
              <span class="total">¥{{ scope.row.total_price }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="添加时间" width="180" />
          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '../../utils/request'

const cartItems = ref([])

const loadCart = async () => {
  try {
    const res = await request.get('/cart/all')
    cartItems.value = res.data || []
  } catch (error) {
    ElMessage.error('加载购物车失败')
  }
}

const handleDelete = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除这个购物车项吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await request.delete(`/cart/${id}`)
    ElMessage.success('删除成功')
    loadCart()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  loadCart()
})
</script>

<style scoped>
.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 15px 0;
  margin-bottom: 20px;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo h1 {
  color: #E02E24;
  font-size: 24px;
}

.nav {
  display: flex;
  gap: 20px;
}

.nav a {
  text-decoration: none;
  color: #333;
  font-size: 16px;
  transition: color 0.3s;
}

.nav a:hover,
.nav a.router-link-active {
  color: #E02E24;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.product-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
}

.total {
  color: #E02E24;
  font-weight: bold;
}
</style>

